package com.apes.framework.plugin.condition.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.condition.model.*;
import com.apes.framework.plugin.condition.repository.ConditionTableRepository;
import com.apes.framework.plugin.condition.repository.FieldCatalogItemRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;

import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 条件表
 *
 * @author wuq
 * @create 2018-11-13 14:34
 */
@Service
public class ConditionTableService extends DomainService {

    @Autowired
    ConditionTableRepository conditionTableRepository;
    @Autowired
    FieldCatalogItemRepository fieldCatalogItemRepository;


    public ConditionTable save(SimpleRequest request) {
        ConditionTable conditionTable = request.getO(ConditionTable.class);
        return conditionTableRepository.saveAndFlush(conditionTable);
    }


    public ConditionTable update(SimpleRequest request) {
        checkData(request);
        ConditionTable conditionTable = request.getO(ConditionTable.class);
        return conditionTableRepository.saveAndFlush(conditionTable);
    }

    public ConditionTable approve(SimpleRequest request) {
        checkData(request);
        ConditionTable conditionTable = request.getO(ConditionTable.class);
        conditionTable.setVaild(true);
        conditionTable = conditionTableRepository.saveAndFlush(conditionTable);
        Map<String, String> models = createEntityScript(conditionTable);
        String sql = "insert into ir_java_source(id,source) values(:id,:source)";
        models.forEach((k, v) -> {
            conditionTableRepository.exec(sql, MapUtil.mapper("id", k, "source", v));
        });
        return conditionTable;
    }

    public String delete(SimpleRequest request) {
        checkData(request);
        ConditionTable conditionTable = request.getO(ConditionTable.class);
        conditionTableRepository.delete(conditionTable);
        return "删除成功";
    }

    public String createEntity(SimpleRequest request) {
        String id = request.get("id");
        ConditionTable conditionTable = conditionTableRepository.findOne(id);
        List<FieldCatalogItem> fieldCatalogItem = fieldCatalogItemRepository.findAllByFieldCatalog_Id("general");
        System.out.println(conditionTable.createEntiy(fieldCatalogItem));
        return "成功";
    }

    public Map<String, String> createEntityScript(ConditionTable conditionTable) {
        List<FieldCatalogItem> fieldCatalogItems = fieldCatalogItemRepository.findAllByFieldCatalog_Id("general");

        return conditionTable.createEntiy(fieldCatalogItems);
    }

    public List<ConditionTable> findVaildConditionTable() {
        Specification<ConditionTable> criteria = JpaDsl.toCriteriaByEq("vaild", true);
        return conditionTableRepository.findAll(criteria);
    }

    public void checkData(SimpleRequest request) {
        String id = request.get("id");

        // 从数据库中取数，检查数据
        ConditionTable oldConditionTable = conditionTableRepository.findOne(id);
        if (oldConditionTable == null) throw new RuntimeException("条件字段【" + id + "】在系统中不存在。");
    }
}
